<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Worker; 

class WGroup extends Base
{
    public function W_group()
    {
global $_G, $_P;
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('管理后台地址不正确');
}
$system=$_G['system'];
$theme = Theme::getThemeById($themeid);
$config = 	Theme::getConfig($themeid);
if(empty($theme['id'])){
	return_msg('管理后台地址不正确');
}
$myworker = Worker::getThemeWorkerByOpenidNew($_G['openid'],$themeid);
if(!($system['ruanjian']!=3 || $config['showshop']==0 || ($system['ruanjian']==3 && $config['showshop']==$myworker['company']))){
	$myworker = "";
}
if (empty($myworker)) {
	return_msg('您不是员工,无权访问');
}
$msg = Worker::gethoutaistats();
if (!empty($msg)) {
	return_msg($msg);
}

$op = !empty($_P['op']) ? $_P['op'] : 'all';
$do = 'group';
$pid = intval($_P['pid']);

if ($myworker['level'] == '1'){	
	if ($op=='all'){
		$tiaojian = "";
	}elseif ($op=='company'){
		$tiaojian = " and r.status > 0 and  w.company = " . $myworker['company'];
	}else{
		$op='my';
		$tiaojian = " and r.status > 0 and  r.workerid = " . $myworker['uid'];
	}
}elseif ($myworker['level'] == '2'){
	if ($op=='all' || $op=='company'){
		$op='all';
		$tiaojian = " and r.status > 0 and  w.company = " . $myworker['company'];
	}else{
		$op='my';
		$tiaojian = " and r.status > 0 and  r.workerid = " . $myworker['uid'];
	}
}else{
	$op='my';
	$tiaojian = " and r.status > 0 and  r.workerid = " . $myworker['uid'];
}


$groupsnum  = db_fetchcolumn ( "SELECT count(*) FROM " . tablename ( t_record ) . " as r
			left join " . tablename ( t_worker ) . " as w on r.workerid = w.uid and r.themeid = w.themeid
			WHERE r.themeid = :themeid " . $tiaojian . " and r.type = 0 and r.status > 0 ", array(":themeid"=>$themeid) );
$total =$groupsnum;

$groupsnumall  = db_fetchcolumn ( "SELECT count(*) FROM " . tablename ( t_record ) . " as r
			left join " . tablename ( t_worker ) . " as w on r.workerid = w.uid and r.themeid = w.themeid
			WHERE r.themeid = :themeid " . $tiaojian . " and r.status > 0 ", array(":themeid"=>$themeid) );
$groupsnumtuanyuan  = db_fetchcolumn ( "SELECT count(*) FROM " . tablename ( t_record ) . " as r
			left join " . tablename ( t_worker ) . " as w on r.workerid = w.uid and r.themeid = w.themeid
			WHERE r.themeid = :themeid " . $tiaojian . " and r.type = 1 and r.status > 0 ", array(":themeid"=>$themeid) );

if ($pid > 0){
	$op='all';
	$tiaojian = " and  r.pid = " . $pid;
}
$pageNumber = max ( 1, intval ( $_P['page'] ) );
$pageSize = 10;
if ($_P['show']=='get') {
	$pageNumber = $pageNumber + 1;
}


$groups  = db_select ( "SELECT  r.*,w.username as workername FROM " . tablename ( t_record ) . " as r
			left join " . tablename ( t_worker ) . " as w on r.workerid = w.uid and r.themeid = w.themeid
			WHERE r.themeid = " . $themeid  . $tiaojian . " and r.type = 0 and r.status > 0 
			order by addtime desc ");//LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize );

foreach ($groups as $index => $item) {
	if (empty($item['uid'])) {
    	$groups[$index]['avatar'] = $_G['URL']['FCWORK_CSS'] . 'images/avatar1.jpg';
    }else{
    	$groups[$index]['avatar'] = tomedia($item['avatar']);
	}
	if ($item['tuannum']>1) {
		$groups[$index]['shenfen']="<span>团长</span>";
	}else{
		$groups[$index]['shenfen']="";
	}

	if ($pid ==0 ){
		$temptxt = "and r.status > 0";
	}else{
		$temptxt = "";
	}
	$tuanlist = db_select ( "SELECT r.*,w.username as workername,w.uid as workerid FROM " . tablename ( t_record ) . " as r
			left join " . tablename ( t_worker ) . " as w on r.workerid = w.uid and r.themeid = w.themeid
			WHERE r.themeid = :themeid and r.status > 0 and r.pid ='" .$item['id']. "' and r.type = 1 ".$temptxt , array(":themeid"=>$themeid) );
	// $groups[$index]['tuannum']=count($tuanlist);
	if (!empty($tuanlist)){
	if($_G['companyname'] =='飞驰软件'){
		$txttemp="";
		foreach ($tuanlist as $key => $row) {
			if (empty($item['uid'])) {
		    	$tuanlist[$index]['avatar'] = $_G['URL']['FCWORK_CSS'] . 'images/avatar1.jpg';
		    }else{
		    	$tuanlist[$index]['avatar'] = tomedia($row['avatar']);
			}

			$txttemp .= "       	<dt>\n";
			$txttemp .= "           	<div class=\"useImg\"><img src=\"" . $tuanlist[$index]['avatar'] . "\"></div>\n";
			$txttemp .= "               <div class=\"peopleTxt\">\n";
			$txttemp .= "                   <h2>" . $row['username'] . "</h2>\n";
			$txttemp .= "                   <p>昵称：".$row['nickname']."</p>\n";
			$txttemp .= "               </div>\n";
			$txttemp .= "               <a href=\"tel:".$row['mobile']."\" class=\"telBtn\"></a>\n";
			$txttemp .= "           </dt>\n";
		}
		$groups[$index]['tuanyuan'] = $txttemp;
	}else{
		$tuanyuan = "";
		foreach ($tuanlist as $key => $row) {
			$xuhao=$key+1;			
		    $tuanyuan = $tuanyuan . "<dl class='ui-flex'>";
		    $tuanyuan = $tuanyuan ."    <dt><img class='warp_img' src='".tomedia($row['avatar'])."''></dt>";
		    $tuanyuan = $tuanyuan ."    <dd>";
		    $tuanyuan = $tuanyuan ."        <p>" . $xuhao . ". " . date("Y-m-d H:i:s", $row['addtime'])."</p>";
		    $tuanyuan = $tuanyuan ."        <p>昵称：".mb_substr($row['nickname'],0,8,'utf-8')."</p>";
		    $tuanyuan = $tuanyuan ."        <p>姓名：".$row['username']."</p>";
		    $tuanyuan = $tuanyuan ."        <p>电话：".$row['mobile']."</p>";
			if(!empty($row['fromuid'])){
				$fans = db_find ( "SELECT * FROM " . tablename ( t_fans ) . " WHERE uid = :uid and themeid =:themeid",array(":themeid"=>$themeid,":uid"=>$row['fromuid']));
				if (!empty($fans['username'])){
					$tuanyuan = $tuanyuan ."        <p>推荐人：".$fans['username']."</p>";
				}elseif (!empty($fans['nickname'])){
					$tuanyuan = $tuanyuan ."        <p>推荐人：".$fans['nickname']."</p>";
				}else{
					$tuanyuan = $tuanyuan ."        <p>推荐人：".$row['fromuid']."</p>";
				}
				
			}else{
				$tuanyuan = $tuanyuan ."        <p>推荐人：无</p>";
			}
			if ($op!='my'){
		    	$tuanyuan = $tuanyuan ."        <p >客服：".$row['workername']."</p>";
		    }elseif ($row['workerid']!=$myworker['uid']){
		    	$tuanyuan = $tuanyuan ."        <p  style='color: #ff0000'>客服：".$row['workername']."</p>";
		    }
		    if ($pid>0) {
		    	if ($row['status'] < 0){
		    		$tuanyuan = $tuanyuan ."        <p >状态：".$item['isrefund']==2?'自动退款':'手工退款'."</p>";
		    	}elseif ($row['status'] == 1){
		    		$tuanyuan = $tuanyuan ."        <p >状态：已支付</p>";
		    	}elseif ($row['status'] == 0){
		    		$tuanyuan = $tuanyuan ."        <p >未支付</p>";
		    	}
		    }
		    $tuanyuan = $tuanyuan ."    </dd>";
		    $tuanyuan = $tuanyuan ."</dl>";
		}
		$groups[$index]['tuanyuan'] = $tuanyuan;
	}
	}else{
		$groups[$index]['tuanyuan']='';
	}
}

                            
if ($_P['show']=='get') {
    $result=  array();
    $result['curPageData'] = $groups;
    $result['totalPage'] =  ceil($total / $pageSize)-1;
    $result['totalSize'] =  $total-$pageSize;

    if ($pageNumber*$pageSize>=$total) {
    	$result['hasNext'] = false ;
    }else{
    	$result['hasNext'] = true  ;
    }
	return_msg($result, '', 'ajax');
}else{
	if($_G['companyname'] =='飞驰软件'){
		$return_clicent = get_defined_vars();
return view('/fcworker/fc_group' , $return_clicent);
	}else{
		$return_clicent = get_defined_vars();
return view('/yxworker/w_group' , $return_clicent);
	}
}
}
}